//
//  InfoView.m
//  Tunnel
//
//  Created by wbb on 2025/6/17.
//

#import "InfoView.h"

@interface YJXYDetailCell : TYBaseTableViewCell

@end
@implementation YJXYDetailCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        
        [self initUI];
    }
    return self;
}

- (void)initUI {
    UILabel *currentLab = nil;
    for (int i = 0; i<4; i++) {
        UILabel *headerLab = [[UILabel alloc] init];
        headerLab.text = @"测试";
        headerLab.tag = 10000000+i;
        headerLab.textColor = main_light_text_color_6;
        headerLab.textAlignment = NSTextAlignmentCenter;
        headerLab.font = [UIFont systemFontOfSize:14];
        [self.contentView addSubview:headerLab];
        [headerLab mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 0) {
                make.left.offset(0);
            }else {
                make.left.equalTo(currentLab.mas_right);
                make.width.equalTo(currentLab.mas_width);
            }
            make.top.bottom.offset(0);
            if (i == 3) {
                make.right.offset(0);
            }
        }];
        
        
        currentLab = headerLab;
    }
}
- (void)cellWithData:(id)data {
    [super cellWithData:data];
    NSString * sjly = @"";
    UILabel *lab1 = (UILabel *)[self.contentView viewWithTag:10000000];
    UILabel *lab2 = (UILabel *)[self.contentView viewWithTag:10000001];
    UILabel *lab3 = (UILabel *)[self.contentView viewWithTag:10000002];
    UILabel *lab4 = (UILabel *)[self.contentView viewWithTag:10000003];
    if([data isKindOfClass:[YJYAContingencyPlanSupplies class]]){
        //预案物资信息
        YJYAContingencyPlanSupplies * m = (YJYAContingencyPlanSupplies*)data;
        lab1.text = m.wzlxStr;
        lab2.text = m.wzmc;
        lab3.text = m.wzsl;
        lab4.text = m.unit;
    }else{
        YJYAContingencyPlanEquipment * m = (YJYAContingencyPlanEquipment*)data;
        lab1.text = m.eqlxStr;
        lab2.text = m.eqmc;
        lab3.text = m.eqsl;
        lab4.text = m.unit;
    }
    
    
}
@end

@interface InfoView ()<UITableViewDelegate,UITableViewDataSource>


@property (nonatomic, strong) UILabel * statusLab;
@property (nonatomic, strong) UILabel * gradeLab;
@property (nonatomic, strong) UILabel * contentLab;
@property (nonatomic, strong) UIImageView * lineImgView;
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, copy) NSArray * dataArr;
@property (nonatomic, strong) UIView * bottomBgView;
@property (nonatomic, strong) UIView * headerView;
@property (nonatomic, assign) NSInteger  currentTag;
@property (nonatomic, strong) YJXYModel*  model;
@property (nonatomic, strong) YJYAModel*  yaModel;
@property (nonatomic, copy) NSArray*  filterArr;

@end
@implementation InfoView

- (instancetype)init {
    self = [super init];
    if(self) {

    }
    return self;
}
-(instancetype)initWithSjModel:(YJXYModel *)sjModel filterArr:(NSArray*)filterArr yaModel:(YJYAModel*)yaModel{
    self = [super init];
    if(self) {
        self.model = sjModel;
        self.yaModel = yaModel;
        self.filterArr = filterArr;
        [self initUI];
    }
    return self;

}

- (void)initUI {
    self.dataArr = self.yaModel.contingencyPlanSuppliesDtoList;
    
    for (YJYAContingencyPlanSupplies * model in self.dataArr) {
        if([model isKindOfClass: YJYAContingencyPlanSupplies.class]){
            for (NSDictionary * dict in self.filterArr[4]) {
                if([dict[@"dictValue"] isEqualToString:model.wzlx]){
                    model.wzlxStr = dict[@"dictLabel"];
                    break;
                }
            }
        }

    }
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    [self addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    TYBoxView *tipBgView = [[TYBoxView alloc] init];
    [scrollView addSubview:tipBgView];
    [tipBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(5);
        make.left.offset(12);
        make.right.offset(-12);
//        make.bottom.equalTo(currentStackView.mas_bottom).offset(5);
        make.width.equalTo(scrollView.mas_width).offset(-24);
        make.height.offset(40);
    }];
    
    UILabel *tipLab = [[UILabel alloc] init];
    tipLab.font = [UIFont systemFontOfSize:17];
    tipLab.textColor = main_color;
 
    if([self.model.sjzt isEqualToString:@"DONE"]){
        tipLab.text = @"已处置";
        tipLab.textColor = hexColor(318C2D);
    }else if ([self.model.sjzt isEqualToString:@"DOING"]){
        tipLab.text = @"处置中";
        tipLab.textColor = hexColor(5490FF);

    }else if ([self.model.sjzt isEqualToString:@"INIT"]){
        tipLab.text = @"待确认";
        tipLab.textColor = hexColor(F3A642);
    }else{
        tipLab.text = @"误报";
        tipLab.textColor = hexColor(FF5454);
    }
    
    [tipBgView addSubview:tipLab];
    self.statusLab = tipLab;
    [tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.centerY.offset(0);
    }];
    
    UILabel *gradeLab = [[UILabel alloc] init];
    gradeLab.font = [UIFont systemFontOfSize:17];
    gradeLab.textColor = hexColor(FF5454);
    gradeLab.text = self.model.sjdj.length >0?[NSString stringWithFormat:@"%@级事件",self.model.sjdj]:@"-";
    [tipBgView addSubview:gradeLab];
    self.gradeLab = gradeLab;
    [gradeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.statusLab.mas_right).offset(15);
        make.centerY.offset(0);
    }];
    
    UILabel *contentLab = [[UILabel alloc] init];
    contentLab.font = [UIFont systemFontOfSize:14];
    contentLab.textColor = main_light_text_color_6;
    NSString * sjlx = @"交通事故";
    for (NSDictionary * dict in self.filterArr[1]) {
        if([dict[@"dictValue"] isEqualToString:self.model.sjlx]){
            sjlx = dict[@"dictLabel"];
            break;
        }
    }
    if(sjlx.length > 0 && self.model.sjdj.length > 0){
        contentLab.text = [NSString stringWithFormat:@"已启用%@%@级应急预案",sjlx,self.model.sjdj];
    }else {
        contentLab.text = @"";
    }
    [tipBgView addSubview:contentLab];
    self.contentLab = contentLab;
    [contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.gradeLab.mas_right).offset(15);
        make.centerY.offset(0);
        make.right.lessThanOrEqualTo(tipBgView.mas_right).offset(-15);
    }];
    
    UIView *topBgView = [[UIView alloc] init];
    topBgView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:topBgView];
    topBgView.layer.shadowColor = [UIColor colorWithRed:156/255.0 green:175/255.0 blue:210/255.0 alpha:0.2].CGColor;
    topBgView.layer.shadowOffset = CGSizeMake(0,4);
    topBgView.layer.shadowOpacity = 1;
    topBgView.layer.shadowRadius = 8;
    topBgView.layer.cornerRadius = 7;
    
    {
        UILabel  *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont systemFontOfSize:17];
        titleLabel.textColor = main_text_color;
        titleLabel.text = @"事件信息";
        [topBgView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(16);
            make.top.offset(12);
        }];
        NSString * sjly = @"";
        for (NSDictionary * dict in self.filterArr[3]) {
            if([dict[@"dictValue"] isEqualToString:self.model.sjly]){
                sjly = dict[@"dictLabel"];
                break;
            }
        }
        NSArray *arr = @[@"事件类型", @"事件方向",@"事件位置",@"涉及车道",@"伤亡人数",@"事件来源",@"事件描述"];
        NSArray *valueArr = @[sjlx, self.model.direction,self.model.pileNo,self.model.laneno,[NSString stringWithFormat:@"%ld死 %ld伤",self.model.death,(long)self.model.injured],sjly,self.model.sjms];
        UIStackView *currentStackView = nil;
        for (int i = 0; i<arr.count; i++) {
            UIStackView *stackView1 = [[UIStackView alloc] init];
            stackView1.spacing = 10;
            [topBgView addSubview:stackView1];
            [stackView1 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.offset(15);
                if(i == 0){
                    make.top.equalTo(titleLabel.mas_bottom).offset(5);
                }else {
                    make.top.equalTo(currentStackView.mas_bottom);
                }
                make.height.offset(20);
            }];
            
            UILabel *leftLab1 = [[UILabel alloc] init];
            leftLab1.text = arr[i];
            leftLab1.textColor = main_light_text_color_9;
            leftLab1.font = [UIFont systemFontOfSize:14];
            
            UILabel * ssxtLab = [[UILabel alloc] init];
            ssxtLab.text = valueArr[i];
            ssxtLab.tag = i+1000;
            ssxtLab.textColor = main_light_text_color_6;
            ssxtLab.font = [UIFont systemFontOfSize:14];
            
            [stackView1 addArrangedSubview:leftLab1];
            [stackView1 addArrangedSubview:ssxtLab];
            
            currentStackView = stackView1;
        }
        
        
        
        [topBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(tipBgView.mas_bottom).offset(15);
            make.left.offset(12);
            make.right.offset(-12);
            make.bottom.equalTo(currentStackView.mas_bottom).offset(5);
            make.width.equalTo(scrollView.mas_width).offset(-24);
        }];
    }
   
    
    
    TYBoxView *bottomBgView = [[TYBoxView alloc] init];
    [scrollView addSubview:bottomBgView];
    self.bottomBgView = bottomBgView;
    
    UIButton *tabBtn1 = [UIButton buttonWithTitle:@"所需物资" titleColor:main_text_color font:[UIFont systemFontOfSize:14]  target:self action:@selector(onType:)];
    tabBtn1.tag = 10;
    [bottomBgView addSubview:tabBtn1];
    [tabBtn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.top.offset(10);
        make.width.equalTo(bottomBgView.mas_width).multipliedBy(0.5);
        make.height.offset(30);
    }];
    
    UIButton *tabBtn2 = [UIButton buttonWithTitle:@"所需载具" titleColor:main_text_color font:[UIFont systemFontOfSize:14]  target:self action:@selector(onType:)];
    tabBtn2.tag = 11;
    [bottomBgView addSubview:tabBtn2];
    [tabBtn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(tabBtn1.mas_right);
        make.centerY.equalTo(tabBtn1.mas_centerY);
        make.width.equalTo(tabBtn1.mas_width);
        make.right.offset(0);
    }];
    
    UIImageView *lineImgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"gjtj_tab_icon"]];
    [bottomBgView addSubview:lineImgView];
    self.lineImgView = lineImgView;
    [lineImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(tabBtn1.mas_bottom).offset(0);
        make.centerX.equalTo(tabBtn1.mas_centerX);
        make.width.offset(20);
        make.height.offset(4);
    }];
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = hexColor(EBF2FF);
    [bottomBgView addSubview:headerView];
    self.headerView = headerView;
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(4);
        make.right.offset(-4);
        make.top.equalTo(lineImgView.mas_bottom).offset(13);
        make.height.offset(24);
    }];
    
    NSArray *headerArr = @[@"物资类型", @"物资名称", @"需要数量",@"计量单位"];
    UILabel *currentLab = nil;
    for (int i = 0; i<headerArr.count; i++) {
        UILabel *headerLab = [[UILabel alloc] init];
        headerLab.text = headerArr[i];
        headerLab.tag = 2000 + i;
        headerLab.textColor = main_light_text_color_6;
        headerLab.textAlignment = NSTextAlignmentCenter;
        headerLab.font = [UIFont systemFontOfSize:14];
        
        [headerView addSubview:headerLab];
        [headerLab mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 0) {
                make.left.offset(0);
            }else {
                make.left.equalTo(currentLab.mas_right);
                make.width.equalTo(currentLab.mas_width);
            }
            make.top.bottom.offset(0);
            if (i == headerArr.count -1) {
                make.right.offset(0);
            }
            
        }];
        
        
        currentLab = headerLab;
    }
    
    [bottomBgView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView.mas_bottom);
        make.left.right.offset(0);
        make.height.offset(self.dataArr.count*24);
        make.bottom.offset(0);
    }];
    
    
    
    [bottomBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topBgView.mas_bottom).offset(15);
        make.left.right.equalTo(topBgView);
        make.bottom.equalTo(scrollView.mas_bottom);
        make.width.equalTo(scrollView.mas_width).offset(-24);
    }];
    
   
    
    [scrollView layoutIfNeeded];
    
}

- (void)onType:(UIButton *)btn {
    NSInteger tag = btn.tag;
    
    
    if(self.currentTag == tag) {
        return;
    }
    NSArray *headerArr = @[@"物资类型", @"物资名称", @"需要数量",@"计量单位"];
    

    if(tag == 10){// select1
        self.dataArr = self.yaModel.contingencyPlanSuppliesDtoList;
        [self.tableView reloadData];
        [self.tableView  mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.headerView.mas_bottom);
            make.left.right.offset(0);
            make.height.offset(self.dataArr.count *24);
            make.bottom.offset(0);
        }];
    }else if (tag == 11){ // select2
        self.dataArr = self.yaModel.contingencyPlanEquipmentDtos;
        for (YJYAContingencyPlanEquipment * model in self.dataArr) {
            if([model isKindOfClass: YJYAContingencyPlanEquipment.class]){
                for (NSDictionary * dict in self.filterArr[5]) {
                    if([dict[@"dictValue"] isEqualToString:model.eqlx]){
                        model.eqlxStr = dict[@"dictLabel"];
                        break;
                    }
                }
            }

        }
        [self.tableView reloadData];
        [self.tableView  mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.headerView.mas_bottom);
            make.left.right.offset(0);
            make.height.offset(self.dataArr.count *24);
            make.bottom.offset(0);
        }];
        headerArr = @[@"机具类型", @"机具名称", @"机具数量",@"计量单位"];
    }
    for (UILabel * label in self.headerView.subviews) {
        label.text = headerArr[label.tag - 2000];
    }
    [self.lineImgView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(btn.mas_bottom).offset(0);
        make.centerX.equalTo(btn.mas_centerX);
        make.width.offset(20);
        make.height.offset(4);
    }];
    self.currentTag = tag;
}
#pragma mark ---- UITableViewDelegate, UITableViewDataSource ----
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 24;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YJXYDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[YJXYDetailCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    
    [cell cellWithData:self.dataArr[indexPath.row]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
#pragma mark ---- lazy ----
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}
@end
